#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=2e5+100;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL col[maxn],c[maxn],a[maxn],dfn[maxn],low[maxn],tot=0,cnt=0;
LL num[maxn];
LL out[maxn];
vector<LL>g[maxn];
bool instack[maxn],vis[maxn];
stack<LL>s;
void tarjan(LL u){
    dfn[u]=low[u]=++tot;
    s.push(u);instack[u]=true;
    for(LL i=0;i<g[u].size();i++){
        LL v=g[u][i];
        if(!dfn[v]){
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(instack[v]) low[u]=min(low[u],dfn[v]);
    }
    if(dfn[u]==low[u]){
        cnt++;
        LL y;
        do{
            y=s.top();
            col[y]=cnt;
            num[cnt]=min(num[cnt],c[y]);
            instack[y]=false;
            s.pop();
        }while(u!=y);
        return;
    }
}
void dfs(LL u){
    vis[u]=true;
    for(LL i=0;i<g[u].size();i++){
        LL v=g[u][i];
        if(vis[v]&&col[v]!=col[u]){
            out[col[u]]++;
        }
        else if(!vis[v]&&col[v]!=col[u]){
            out[col[u]]++;
            dfs(v);
        }
        else if(!vis[v]&&col[v]==col[u]) dfs(v);
    }
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n;cin>>n;
  memset(num,0x3f,sizeof(num));
  for(LL i=1;i<=n;i++) cin>>c[i];
  for(LL i=1;i<=n;i++) cin>>a[i];
  for(LL i=1;i<=n;i++) g[i].push_back(a[i]);
  for(LL i=1;i<=n;i++){
    if(!dfn[i]){
        tarjan(i);
    }
  }
  for(LL i=1;i<=n;i++){
    if(!vis[i]){
        dfs(i);
    }
  }
  LL sum=0;
  for(LL i=1;i<=cnt;i++){
    if(out[i]==0){
        sum+=num[i];
    }
  }
  cout<<sum<<"\n";
return 0;
}
